﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo_2D
{
    //标签与各个基站距离
    struct AllPlen
    {
        public int fnum; //数据帧序列号
        public int l_1;//标签与基站1距离
        public int l_2;//标签与基站2距离
        public int l_3;//标签与基站3距离
        public int l_4;//标签与基站4距离
    };

    class DeviceInf
    {
        public List<byte> rdat; //接收数据缓冲
        public List<AllPlen> rpl; //接收数据解析长度缓冲

        public static int F_TAG_ADDR_OFT = 3;
        public static int F_NUM_OFT = F_TAG_ADDR_OFT + 1;
        public static int F_DIS_1 = F_NUM_OFT + 2;
        public static int F_DIS_2 = F_DIS_1 + 2;
        public static int F_DIS_3 = F_DIS_2 + 2;

        /// <summary>
        /// Deviceinf 构造函数
        /// </summary>
        public DeviceInf()
        {
            rdat = new List<byte>();
            rpl = new List<AllPlen>();
        }

        private int TwoByteToUshort(byte l, byte h)
        {
            int ret = 0;
   
            ret = (int)((int)h * 256 + (int)l);

            return ret;
        }

        /// <summary>
        /// 接收数据解析函数,将接收字符解析到长度
        /// </summary>
        public bool ParseRecv()
        {
            bool ret = false;
            AllPlen pinf = new AllPlen();

            if (rdat.Count >= 12) //有效数据帧
            {
                //获取标签ID
                Console.WriteLine("数据帧TAG_ID: {0} ", rdat[F_TAG_ADDR_OFT]);
                //获取帧序列号
                pinf.fnum = TwoByteToUshort(rdat[F_NUM_OFT], rdat[F_NUM_OFT + 1]);
                //获取第一距离值
                pinf.l_1 = TwoByteToUshort(rdat[F_DIS_1], rdat[F_DIS_1 + 1]);
                //获取第二距离值
                pinf.l_2 = TwoByteToUshort(rdat[F_DIS_2], rdat[F_DIS_2 + 1]);
                //获取第三距离值
                pinf.l_3 = TwoByteToUshort(rdat[F_DIS_3], rdat[F_DIS_3 + 1]);

                rpl.Add(pinf);

                ret = true;
            }
            else
            {
                ret = false;
            }

            return ret;
        }

      
    }
}
